/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import java.sql.*;
import java.util.Vector;
import Beans.NgonNgu;

/**
 *
 * @author VIC
 */
public class NgonNgu_Model {

    public NgonNgu_Model() {
    }

    public static NgonNgu[] convertToArray(Vector v) {
        int n = v.size();
        NgonNgu[] arr = new NgonNgu[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (NgonNgu) v.get(i);
        }
        return arr;
    }
    /* HÃ m nÃ y tráº£ vá» danh sÃ¡ch ngÃ´n ngá»¯ Ä‘ang cÃ³ trong há»‡ thá»‘ng */

    public static NgonNgu[] DSNgonNgu() {

        Vector dsnn = new Vector();// biáº¿n dsnn lÆ°u danh sÃ¡ch ngÃ´n ngá»¯ hiá»‡n cÃ³.
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = "Select * from NgonNgu order by macdinh desc";
            rss = stmt.executeQuery(query);
            //rss = ConnectionPool.Select(query);
            while (rss.next()) {
                NgonNgu nn = new NgonNgu();
                nn.setMaNN(rss.getInt(1));
                nn.setTenNN(rss.getString(2));
                nn.setHinhAnh(rss.getString(3));
                nn.setMacDinh(rss.getByte(4));
                dsnn.add(nn);
            }
            stmt.close();
            rss.close();
            conn.close();
            return convertToArray(dsnn);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }
    // HÃ m láº¥y ngÃ´n ngá»¯ máº·c Ä‘á»‹nh hiá»‡n táº¡i.

    public static int NgonnguMD() {

        int nnmd = 1;
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = "Select MaNN from NgonNgu where MacDinh = 1";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                nnmd = rss.getInt(1);
            }
            stmt.close();
            rss.close();
            conn.close();
            return nnmd;
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }

    }
    // HÃ m láº¥y mÃ£ ngÃ´n ngá»¯ tá»« tÃªn ngÃ´n ngá»¯.

    public static int MaNgonNgu(String tennn) {

        int nnmd = 1;// biáº¿n nnmd lÆ°u mÃ£ cá»§a ngÃ´n ngá»¯ máº·c Ä‘á»‹nh.
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            query = "Select MaNN from NgonNgu where tennn = '" + tennn + "'";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                nnmd = rss.getInt(1);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return nnmd;
    }

    public static String TenNgonNgu(int mann) {

        String tennn = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "Select TenNN from NgonNgu where MaNN = " + mann;
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                tennn = rss.getString(1);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return tennn;
    }
}
